package cz.khfotbal.beans;

import java.io.IOException;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
import java.util.Date;
import java.util.List;

import javax.faces.context.FacesContext;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import cz.khfotbal.dao.FrontMatchDao;
import cz.khfotbal.dao.FrontMatchDatesDao;
import cz.khfotbal.dao.FrontMatchRoundDao;
import cz.khfotbal.dao.SeasonCompetionDao;
import cz.khfotbal.dao.TableDao;
import cz.khfotbal.domain.Competion;
import cz.khfotbal.domain.FrontMatch;
import cz.khfotbal.domain.FrontMatchDates;
import cz.khfotbal.domain.FrontMatchRounds;
import cz.khfotbal.domain.FrontTable;
import cz.khfotbal.domain.Match;
import cz.khfotbal.domain.Season;
import cz.khfotbal.domain.SeasonCompetion;
import cz.khfotbal.domain.Team;
import cz.khfotbal.service.CompetionService;
import cz.khfotbal.service.MatchService;
import cz.khfotbal.service.SeasonService;
import cz.khfotbal.service.TeamService;
import cz.khfotbal.utils.KHFotbalUtils;

@Component
@Scope(value = "session")
public class SearchTeamBean {

	@Autowired
	private SeasonCompetionDao seasonCompetionDao;
	@Autowired
	private TableDao tableDao;
	@Autowired
	private TeamService teamService;
	@Autowired
	private SeasonService seasonService;
	@Autowired
	private CompetionService competionService;
	@Autowired
	private MatchService matchService;
	@Autowired
	private FrontMatchRoundDao frontMatchRoundsDao;
	@Autowired
	private FrontMatchDatesDao frontMatchDatesDao;
	@Autowired
	private FrontMatchDao frontMatchDao;

	private List<String> seasonNameList = new ArrayList<String>();
	private List<Season> seasonList = new ArrayList<Season>();
	private String selectedSeasonName = "";
	private Season selectedSeason;
	private List<String> competionNameList = new ArrayList<String>();
	private List<Competion> competionList = new ArrayList<Competion>();
	private String selectedCompetionName = "";
	private Competion selectedCompetion;
	private Team selectedTeam = new Team();
	private List<Team> teamList = new ArrayList<Team>();
	private List<Team> filteredTeams = new ArrayList<Team>();
	private List<Team> teamListAdd = new ArrayList<Team>();
	private Team selectedTeamAdd;
	private List<Match> matchList = new ArrayList<Match>();
	private List<Match> matchFilteredList = new ArrayList<Match>();
	private Match selectedMatch = new Match();
	private Integer selectedMatchId;
	private boolean afterSearch = true;
	private Integer selectedRound = 1;
	private List<Integer> rounds = new ArrayList<Integer>();
	private List<String> matchDates = new ArrayList<String>();
	private String seasonGUI = "";
	private String competionGUI = "";
	private String seasonID;

	private int number;
	private String playDay = "";
	private String playTime = "";
	public int lastId;

	/* vlozeno pro optimalizaci */
	private List<FrontTable> frontTable = new ArrayList<FrontTable>();

	public List<String> getDomaciStrelci() {
		if (selectedMatch.getDomaciStrelci() == null) {
			return new ArrayList<String>();
		}
		return new ArrayList<String>(
				Arrays.asList(selectedMatch.getDomaciStrelci().split(
						System.getProperty("line.separator"))));
	}

	public List<String> getHosteStrelci() {
		if (selectedMatch.getHosteStrelci() == null) {
			return new ArrayList<String>();
		}
		return new ArrayList<String>(Arrays.asList(selectedMatch
				.getHosteStrelci().split(System.getProperty("line.separator"))));
	}

	public List<String> getDomaciZaklSestava() {
		if (selectedMatch.getDomaciZaklSestava() == null) {
			return new ArrayList<String>();
		}
		return new ArrayList<String>(Arrays.asList(selectedMatch
				.getDomaciZaklSestava().split(
						System.getProperty("line.separator"))));
	}

	public List<String> getHosteZaklSestava() {
		if (selectedMatch.getHosteZaklSestava() == null) {
			return new ArrayList<String>();
		}
		return new ArrayList<String>(Arrays.asList(selectedMatch
				.getHosteZaklSestava().split(
						System.getProperty("line.separator"))));
	}

	public List<String> getDomaciStridani() {
		if (selectedMatch.getDomaciStridani() == null) {
			return new ArrayList<String>();
		}
		return new ArrayList<String>(Arrays.asList(selectedMatch
				.getDomaciStridani()
				.split(System.getProperty("line.separator"))));
	}

	public List<String> getHosteStridani() {
		if (selectedMatch.getHosteStridani() == null) {
			return new ArrayList<String>();
		}
		return new ArrayList<String>(
				Arrays.asList(selectedMatch.getHosteStridani().split(
						System.getProperty("line.separator"))));
	}

	public List<String> getDomaciZluteKarty() {
		if (selectedMatch.getDomaciZluteKarty() == null) {
			return new ArrayList<String>();
		}
		return new ArrayList<String>(Arrays.asList(selectedMatch
				.getDomaciZluteKarty().split(
						System.getProperty("line.separator"))));
	}

	public List<String> getHosteZluteKarty() {
		if (selectedMatch.getHosteZluteKarty() == null) {
			return new ArrayList<String>();
		}
		return new ArrayList<String>(Arrays.asList(selectedMatch
				.getHosteZluteKarty().split(
						System.getProperty("line.separator"))));
	}

	public List<String> getDomaciCerveneKarty() {
		if (selectedMatch.getDomaciCerveneKarty() == null) {
			return new ArrayList<String>();
		}
		return new ArrayList<String>(Arrays.asList(selectedMatch
				.getDomaciCerveneKarty().split(
						System.getProperty("line.separator"))));
	}

	public List<String> getHosteCerveneKarty() {
		if (selectedMatch.getHosteCerveneKarty() == null) {
			return new ArrayList<String>();
		}
		return new ArrayList<String>(Arrays.asList(selectedMatch
				.getHosteCerveneKarty().split(
						System.getProperty("line.separator"))));
	}

	public void setupSelectedMatch() {
		for (Match m : matchList) {
			if (m.getId() == selectedMatchId)
				selectedMatch = m;
		}
	}

	public void deleteTeamFromCompetion() {
		teamService.deleteTeamBySeasonAndCompetion(selectedSeason,
				selectedCompetion, selectedTeam);
		fillTeamList();
	}

	public void handleSeasonChange() {
		if (selectedSeasonName.equals("")) {
			competionNameList = new ArrayList<String>();
			return;
		}
		competionList = competionService
				.getCompetionsBySeason(getSeasonByName(selectedSeasonName));
		competionNameList = new ArrayList<String>();
		for (Competion c : competionList) {
			competionNameList.add(c.getName());
		}
	}

	public Season getSeasonByName(String name) {
		for (Season s : seasonList) {
			if (name.equals(s.getSeasonName())) {
				return s;
			}
		}
		return null;
	}

	public Competion getCompetionByName(String name) {
		for (Competion c : competionList) {
			if (name.equals(c.getName())) {
				return c;
			}
		}
		return null;
	}

	public void fillTeamList() {
		selectedSeason = getSeasonByName(selectedSeasonName);
		selectedCompetion = getCompetionByName(selectedCompetionName);
		teamList = teamService.getTeamsBySeasonAndCompetion(selectedSeason,
				selectedCompetion);
		List<Team> all = teamService.getAllTeams();
		teamListAdd = new ArrayList<Team>();
		for (Team t : all) {
			boolean found = false;
			for (Team added : teamList) {
				if (t.getName().equals(added.getName())) {
					found = true;
				}
			}
			if (!found) {
				teamListAdd.add(t);
			}
		}
		selectedTeam = new Team();
	}

	public Team getTeamFromListById(int id) {
		for (Team t : teamList) {
			if (t.getId() == id)
				return t;
		}
		return null;
	}

	public void selectRoundAction() {
		matchFilteredList = new ArrayList<Match>();
		matchDates = new ArrayList<String>();
		for (Match m : matchList) {
			if (m.getRound() == selectedRound) {
				matchFilteredList.add(m);
				if (!KHFotbalUtils
						.containsString(matchDates, m.getMatchStart())) {
					matchDates.add(m.getMatchStart());
				}
			}
		}
		Collections.sort(matchDates, new MatchDateComparator());
		Collections.sort(matchFilteredList, new MatchTimeComparator());
	}

	public void setKrajskyPrebor() {
		Season season = seasonService.getSeasonById(1);
		selectedSeasonName = season.getSeasonName();
		selectedCompetionName = competionService.getCompetionById(4).getName();
		fillTeamListWithResultData();
	}

	public void setATrida() {
		Season season = seasonService.getSeasonById(1);
		selectedSeasonName = season.getSeasonName();
		selectedCompetionName = competionService.getCompetionById(5).getName();
		fillTeamListWithResultData();
	}

	public void setBTridaA() {
		Season season = seasonService.getSeasonById(1);
		selectedSeasonName = season.getSeasonName();
		selectedCompetionName = competionService.getCompetionById(6).getName();
		fillTeamListWithResultData();
	}

	public void setBTridaB() {
		Season season = seasonService.getSeasonById(1);
		selectedSeasonName = season.getSeasonName();
		selectedCompetionName = competionService.getCompetionById(7).getName();
		fillTeamListWithResultData();
	}

	public void fillTeamListWithResultData() {
		// selectedSeason = getSeasonByName(selectedSeasonName);
		// selectedCompetion = getCompetionByName(selectedCompetionName);
		// SeasonCompetion sc = seasonCompetionDao
		// .findSeasonCopetionBySeasonIdAndCometionId(
		// selectedSeason.getId(), selectedCompetion.getId());
		// frontTable = tableDao.getListTables(sc.getId());
		//
		// matchFilteredList = matchService.getMatchesBySeasonAndCompetion(
		// selectedSeason, selectedCompetion);
		// rounds = new ArrayList<Integer>();
		// if (matchFilteredList.size() > 0) {
		// for (int i = 0; i < matchFilteredList.get(
		// matchFilteredList.size() - 1).getRound(); i++) {
		// rounds.add(i + 1);
		// }
		// }
		// matchDates = new ArrayList<String>();
		// for (Match m : matchFilteredList) {
		// boolean added = false;
		// for (String s : matchDates)
		// if (s.equals(m.getMatchStart())) {
		// added = true;
		// }
		// if (!added) {
		// matchDates.add(m.getMatchStart());
		// }
		// }
		// seasonGUI = new String(selectedSeason.getSeasonName());
		// competionGUI = new String(selectedCompetion.getName());
		// teamList = teamService.getTeamsBySeasonAndCompetion(selectedSeason,
		// selectedCompetion);
		// for (Match match : matchFilteredList) {
		// if ((match.getHomeTeamScore() != null && match.getAwayTeamScore() !=
		// null)
		// && (KHFotbalUtils.isInteger(match.getHomeTeamScore()) &&
		// (KHFotbalUtils
		// .isInteger(match.getAwayTeamScore())))) {
		// getTeamFromListById(match.getHomeTeam().getId())
		// .getResultData()
		// .setMatches(
		// getTeamFromListById(match.getHomeTeam().getId())
		// .getResultData().getMatches() + 1);
		// getTeamFromListById(match.getAwayTeam().getId())
		// .getResultData()
		// .setMatches(
		// getTeamFromListById(match.getAwayTeam().getId())
		// .getResultData().getMatches() + 1);
		// getTeamFromListById(match.getHomeTeam().getId())
		// .getResultData()
		// .setScoredGoals(
		// getTeamFromListById(match.getHomeTeam().getId())
		// .getResultData().getScoredGoals()
		// + Integer.valueOf(match
		// .getHomeTeamScore()));
		// getTeamFromListById(match.getAwayTeam().getId())
		// .getResultData()
		// .setScoredGoals(
		// getTeamFromListById(match.getAwayTeam().getId())
		// .getResultData().getScoredGoals()
		// + Integer.valueOf(match
		// .getAwayTeamScore()));
		// getTeamFromListById(match.getHomeTeam().getId())
		// .getResultData()
		// .setCollectedGoals(
		// getTeamFromListById(match.getHomeTeam().getId())
		// .getResultData().getCollectedGoals()
		// + Integer.valueOf(match
		// .getAwayTeamScore()));
		// getTeamFromListById(match.getAwayTeam().getId())
		// .getResultData()
		// .setCollectedGoals(
		// getTeamFromListById(match.getAwayTeam().getId())
		// .getResultData().getCollectedGoals()
		// + Integer.valueOf(match
		// .getHomeTeamScore()));
		// if (Integer.valueOf(match.getHomeTeamScore()) > Integer
		// .valueOf(match.getAwayTeamScore())) {
		// getTeamFromListById(match.getHomeTeam().getId())
		// .getResultData().setPoints(
		// getTeamFromListById(
		// match.getHomeTeam().getId())
		// .getResultData().getPoints() + 3);
		// getTeamFromListById(match.getHomeTeam().getId())
		// .getResultData().setWins(
		// getTeamFromListById(
		// match.getHomeTeam().getId())
		// .getResultData().getWins() + 1);
		// getTeamFromListById(match.getAwayTeam().getId())
		// .getResultData().setLoss(
		// getTeamFromListById(
		// match.getAwayTeam().getId())
		// .getResultData().getLoss() + 1);
		// }
		// if (Integer.valueOf(match.getHomeTeamScore()) < Integer
		// .valueOf(match.getAwayTeamScore())) {
		// getTeamFromListById(match.getAwayTeam().getId())
		// .getResultData().setPoints(
		// getTeamFromListById(
		// match.getAwayTeam().getId())
		// .getResultData().getPoints() + 3);
		// getTeamFromListById(match.getHomeTeam().getId())
		// .getResultData().setLoss(
		// getTeamFromListById(
		// match.getHomeTeam().getId())
		// .getResultData().getLoss() + 1);
		// getTeamFromListById(match.getAwayTeam().getId())
		// .getResultData().setWins(
		// getTeamFromListById(
		// match.getAwayTeam().getId())
		// .getResultData().getWins() + 1);
		// }
		// if (Integer.valueOf(match.getHomeTeamScore()) == Integer
		// .valueOf(match.getAwayTeamScore())) {
		// getTeamFromListById(match.getHomeTeam().getId())
		// .getResultData().setPoints(
		// getTeamFromListById(
		// match.getHomeTeam().getId())
		// .getResultData().getPoints() + 1);
		// getTeamFromListById(match.getAwayTeam().getId())
		// .getResultData().setPoints(
		// getTeamFromListById(
		// match.getAwayTeam().getId())
		// .getResultData().getPoints() + 1);
		// getTeamFromListById(match.getHomeTeam().getId())
		// .getResultData().setDraws(
		// getTeamFromListById(
		// match.getHomeTeam().getId())
		// .getResultData().getDraws() + 1);
		// getTeamFromListById(match.getAwayTeam().getId())
		// .getResultData().setDraws(
		// getTeamFromListById(
		// match.getAwayTeam().getId())
		// .getResultData().getDraws() + 1);
		// }
		//
		// }
		//
		// }
		// srovnani podle bodu
		// Collections.sort(teamList, new HomeGoalsComparator());
		// Collections.sort(teamList, new DifferenceComparator());
		// Collections.sort(teamList, new PointsComparator());
		// for (int i = 0; i < teamList.size(); i++) {
		// teamList.get(i).getResultData().setPosition(i + 1);
		// }
		// selectedTeam = new Team();
		afterSearch = true;
		if (frontTable.size() < 0) {
			afterSearch = false;
		}
		selectedCompetionName = "";
		selectedSeasonName = "";
		competionNameList = new ArrayList<String>();
		competionList = new ArrayList<Competion>();
	}

	public void pokus() {
		selectedSeason = getSeasonByName(selectedSeasonName);
		selectedCompetion = getCompetionByName(selectedCompetionName);
		SeasonCompetion sc = seasonCompetionDao
				.findSeasonCopetionBySeasonIdAndCometionId(
						selectedSeason.getId(), selectedCompetion.getId());
		selectedCompetionName = "";
		selectedSeasonName = "";
		try {

			// FacesContext
			// .getCurrentInstance()
			// .getExternalContext()
			// .redirect(
			// "http://www.khfotbal.cz/khfotbal/user/rozpis.xhtml?id="
			// + sc.getId() + "_1");

			int page = 1;
			if (sc.getId() < 6) {
				page = 2;
			}

			FacesContext
					.getCurrentInstance()
					.getExternalContext()
					.redirect(
							"http://www.khfotbal.cz/khfotbal/user/rozpis.xhtml?id="
									+ sc.getId() + "_" + page);

		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	public List<String> getSeasonNamesList() {
		seasonNameList = new ArrayList<String>();
		if (seasonList.isEmpty()) {
			seasonList = seasonService.getAllSeasons();
		}
		for (Season season : seasonList) {
			seasonNameList.add(season.getSeasonName());
		}
		return seasonNameList;
	}

	public void addTeamToCompetion() {
		teamService.addTeamToCompetion(selectedTeamAdd, selectedCompetion,
				selectedSeason, number, playDay, playTime);
		fillTeamList();
	}

	public void updateTeamToCompetion() {

		teamService.updateTeamToCompetion(selectedTeam, selectedCompetion,
				selectedSeason, selectedTeam.getNumber(),
				selectedTeam.getPlayDay(), selectedTeam.getPlayTime());
		fillTeamList();
	}

	public void setSeasonNamesList(List<String> seasonNamesList) {
		this.seasonNameList = seasonNamesList;
	}

	public List<Season> getSeasonList() {
		return seasonList;
	}

	public void setSeasonList(List<Season> seasonList) {
		this.seasonList = seasonList;
	}

	public String getSelectedSeasonName() {
		return selectedSeasonName;
	}

	public void setSelectedSeasonName(String selectedSeasonName) {
		this.selectedSeasonName = selectedSeasonName;
	}

	public Season getSelectedSeason() {
		return selectedSeason;
	}

	public void setSelectedSeason(Season selectedSeason) {
		this.selectedSeason = selectedSeason;
	}

	public List<String> getCompetionNameList() {
		competionNameList = new ArrayList<String>();
		if (competionList.isEmpty()) {
			competionList = competionService.getAllCompetions();
		}
		for (Competion competion : competionList) {
			competionNameList.add(competion.getName());
		}

		return competionNameList;
	}

	public void setCompetionNameList(List<String> competionNamesList) {
		this.competionNameList = competionNamesList;
	}

	public List<Competion> getCompetionList() {
		return competionList;
	}

	public void setCompetionList(List<Competion> competionList) {
		this.competionList = competionList;
	}

	public String getSelectedCompetionName() {
		return selectedCompetionName;
	}

	public void setSelectedCompetionName(String selectedCompetionName) {
		this.selectedCompetionName = selectedCompetionName;
	}

	public Competion getSelectedCompetion() {
		return selectedCompetion;
	}

	public void setSelectedCompetion(Competion selectedCompetion) {
		this.selectedCompetion = selectedCompetion;
	}

	public List<String> getSeasonNameList() {
		return seasonNameList;
	}

	public void setSeasonNameList(List<String> seasonNameList) {
		this.seasonNameList = seasonNameList;
	}

	public Team getSelectedTeam() {
		return selectedTeam;
	}

	public void setSelectedTeam(Team selectedTeam) {
		this.selectedTeam = selectedTeam;
	}

	public List<Team> getTeamList() {
		return teamList;
	}

	public void setTeamList(List<Team> teamList) {
		this.teamList = teamList;
	}

	public List<Team> getFilteredTeams() {
		return filteredTeams;
	}

	public void setFilteredTeams(List<Team> filteredTeams) {
		this.filteredTeams = filteredTeams;
	}

	public TeamService getTeamService() {
		return teamService;
	}

	public void setTeamService(TeamService teamService) {
		this.teamService = teamService;
	}

	public List<Team> getTeamListAdd() {
		return teamListAdd;
	}

	public void setTeamListAdd(List<Team> teamListAdd) {
		this.teamListAdd = teamListAdd;
	}

	public Team getSelectedTeamAdd() {
		return selectedTeamAdd;
	}

	public void setSelectedTeamAdd(Team selectedTeamAdd) {
		this.selectedTeamAdd = selectedTeamAdd;
	}

	public int getNumber() {
		return number;
	}

	public void setNumber(int number) {
		this.number = number;
	}

	public String getPlayDay() {
		return playDay;
	}

	public void setPlayDay(String playDay) {
		this.playDay = playDay;
	}

	public String getPlayTime() {
		return playTime;
	}

	public void setPlayTime(String playTime) {
		this.playTime = playTime;
	}

	public SeasonService getSeasonService() {
		return seasonService;
	}

	public void setSeasonService(SeasonService seasonService) {
		this.seasonService = seasonService;
	}

	public CompetionService getCompetionService() {
		return competionService;
	}

	public void setCompetionService(CompetionService competionService) {
		this.competionService = competionService;
	}

	public MatchService getMatchService() {
		return matchService;
	}

	public void setMatchService(MatchService matchService) {
		this.matchService = matchService;
	}

	public class PointsComparator implements Comparator<Team> {
		public int compare(Team t1, Team t2) {
			return (t2.getResultData().getPoints() - t1.getResultData()
					.getPoints());
		}

	}

	public class DifferenceComparator implements Comparator<Team> {
		public int compare(Team t1, Team t2) {
			return ((t2.getResultData().getScoredGoals() - t2.getResultData()
					.getCollectedGoals()) - (t1.getResultData()
					.getScoredGoals() - t1.getResultData().getCollectedGoals()));
		}

	}

	public class HomeGoalsComparator implements Comparator<Team> {
		public int compare(Team t1, Team t2) {
			return (t2.getResultData().getScoredGoals() - t1.getResultData()
					.getScoredGoals());
		}

	}

	public class MatchTimeComparator implements Comparator<Match> {

		public int compare(Match m1, Match m2) {
			String time1 = m1.getStart();
			String time2 = m2.getStart();
			DateFormat sdf = new SimpleDateFormat("hh:mm");
			Date date1;
			Date date2;
			try {
				date1 = sdf.parse(time1);
				date2 = sdf.parse(time2);
			} catch (ParseException e) {
				return -1;
			}
			return date1.compareTo(date2);
		}
	}

	public class MatchDateComparator implements Comparator<String> {

		public int compare(String o1, String o2) {
			String date1 = StringUtils.substringAfter(o1, ",").trim();
			String date2 = StringUtils.substringAfter(o2, ",").trim();
			Date d1 = KHFotbalUtils.getDateFromString(date1);
			Date d2 = KHFotbalUtils.getDateFromString(date2);
			return d1.compareTo(d2);
		}

	}

	public List<Match> getMatchList() {
		return matchList;
	}

	public void setMatchList(List<Match> matchList) {
		this.matchList = matchList;
	}

	public List<Match> getMatchFilteredList() {
		return matchFilteredList;
	}

	public void setMatchFilteredList(List<Match> matchFilteredList) {
		this.matchFilteredList = matchFilteredList;
	}

	public Match getSelectedMatch() {
		return selectedMatch;
	}

	public void setSelectedMatch(Match selectedMatch) {
		this.selectedMatch = selectedMatch;
	}

	public Integer getSelectedMatchId() {
		return selectedMatchId;
	}

	public void setSelectedMatchId(Integer selectedMatchId) {
		this.selectedMatchId = selectedMatchId;
	}

	public boolean isAfterSearch() {
		return afterSearch;
	}

	public void setAfterSearch(boolean afterSearch) {
		this.afterSearch = afterSearch;
	}

	public Integer getSelectedRound() {
		return selectedRound;
	}

	public void setSelectedRound(Integer selectedRound) {
		this.selectedRound = selectedRound;
	}

	public List<Integer> getRounds() {
		String parameter = FacesContext.getCurrentInstance()
				.getExternalContext().getRequestParameterMap().get("id");
		int id = Integer.parseInt(parameter);
		FrontMatchRounds newFmr = frontMatchRoundsDao.getFrontMatchRound(id);
		int pom = newFmr.getRounds();

		rounds = new ArrayList<Integer>();
		if (matchFilteredList.size() > 0) {
			for (int i = 0; i < pom; i++) {
				rounds.add(i + 1);
			}
		}
		return rounds;
	}

	public void setRounds(List<Integer> rounds) {
		this.rounds = rounds;
	}

	public List<String> getMatchDates() {
		String parameter = FacesContext.getCurrentInstance()
				.getExternalContext().getRequestParameterMap().get("id");
		int id;
		int id2;
		try {
			String[] split = parameter.split("_");
			id = Integer.parseInt(split[0]);
			id2 = Integer.parseInt(split[1]);
		} catch (Exception e) {
			id = 0;
			id2 = 0;
		}

		// String parameter2 = FacesContext.getCurrentInstance()
		// .getExternalContext().getRequestParameterMap().get("page");
		// int page = Integer.parseInt(parameter2);
		matchDates = new ArrayList<String>();
		List<FrontMatchDates> newFmds = frontMatchDatesDao
				.getListFrontMatchDates(id, id2);
		for (FrontMatchDates date : newFmds) {
			matchDates.add(date.getDate());
		}
		return matchDates;
	}

	public List<FrontMatch> getFrontMatch() {
		String parameter = FacesContext.getCurrentInstance()
				.getExternalContext().getRequestParameterMap().get("id");
		String[] split = parameter.split("_");
		int id = Integer.parseInt(split[0]);
		int id2 = Integer.parseInt(split[1]);
		// String parameter2 = FacesContext.getCurrentInstance()
		// .getExternalContext().getRequestParameterMap().get("page");
		// int page = Integer.parseInt(parameter2);
		List<FrontMatch> newFms = frontMatchDao.getListMatches(id, id2);
		return newFms;
	}

	public void setMatchDates(List<String> matchDates) {
		this.matchDates = matchDates;
	}

	public String getSeasonGUI() {
		String parameter = FacesContext.getCurrentInstance()
				.getExternalContext().getRequestParameterMap().get("id");
		String[] split = parameter.split("_");
		int id = Integer.parseInt(split[0]);
		SeasonCompetion sc = seasonCompetionDao.getById(id);
		seasonGUI = sc.getSeason().getSeasonName();
		return seasonGUI;
	}

	public String getSeasonGUIDetail() {
		String parameter = FacesContext.getCurrentInstance()
				.getExternalContext().getRequestParameterMap().get("id");
		String[] split = parameter.split("_");
		int id = Integer.parseInt(split[0]);
		SeasonCompetion sc = seasonCompetionDao.getById(id);
		seasonGUI = sc.getSeason().getSeasonName();
		return seasonGUI;
	}

	public void setSeasonGUI(String seasonGUI) {
		this.seasonGUI = seasonGUI;
	}

	public String getCompetionGUI() {
		String parameter = FacesContext.getCurrentInstance()
				.getExternalContext().getRequestParameterMap().get("id");
		String[] split = parameter.split("_");
		int id = Integer.parseInt(split[0]);
		SeasonCompetion sc = seasonCompetionDao.getById(id);
		competionGUI = sc.getCompetion().getName();
		return competionGUI;
	}

	public String getCompetionGUIDetail() {
		String parameter = FacesContext.getCurrentInstance()
				.getExternalContext().getRequestParameterMap().get("id");
		String[] split = parameter.split("_");
		int id = Integer.parseInt(split[0]);
		SeasonCompetion sc = seasonCompetionDao.getById(id);
		competionGUI = sc.getCompetion().getName();
		return competionGUI;
	}

	public void setCompetionGUI(String competionGUI) {
		this.competionGUI = competionGUI;
	}

	/* vlozeno */
	public List<FrontTable> getFrontTable() {
		String parameter = FacesContext.getCurrentInstance()
				.getExternalContext().getRequestParameterMap().get("id");
		int id;
		try {
			String[] split = parameter.split("_");
			id = Integer.parseInt(split[0]);
		} catch (Exception e) {
			id = 0;
		}
		frontTable = tableDao.getListTables(id);
		//
		// SeasonCompetion sc = seasonCompetionDao.getById(id);
		// matchFilteredList = matchService.getMatchesBySeasonAndCompetion(
		// sc.getSeason(), sc.getCompetion());
		// competionGUI = sc.getCompetion().getName();
		// seasonGUI = sc.getSeason().getSeasonName();
		// rounds = new ArrayList<Integer>();
		// if (matchFilteredList.size() > 0) {
		// for (int i = 0; i < matchFilteredList.get(
		// matchFilteredList.size() - 1).getRound(); i++) {
		// rounds.add(i + 1);
		// }
		// }
		// matchDates = new ArrayList<String>();
		// for (Match m : matchFilteredList) {
		// boolean added = false;
		// for (String s : matchDates)
		// if (s.equals(m.getMatchStart())) {
		// added = true;
		// }
		// if (!added) {
		// matchDates.add(m.getMatchStart());
		// }
		// }
		//
		// if (lastId != id) {
		// lastId = id;
		// try {
		// FacesContext
		// .getCurrentInstance()
		// .getExternalContext()
		// .redirect(
		// "http://localhost:8080/khfotbal/user/rozpis.xhtml?id="
		// + id);
		// } catch (IOException e) {
		// e.printStackTrace();
		// }
		// }
		return frontTable;

	}

	public void setFrontTable(List<FrontTable> frontTable) {
		this.frontTable = frontTable;
	}

	public Match getMatch() {
		String parameter = FacesContext.getCurrentInstance()
				.getExternalContext().getRequestParameterMap().get("id");
		String[] split = parameter.split("_");
		int id = Integer.parseInt(split[1]);
		return matchService.getMatchById(id);
	}

	public String getSeasonId() {
		String parameter = FacesContext.getCurrentInstance()
				.getExternalContext().getRequestParameterMap().get("id");
		String result;
		try {
			String[] split = parameter.split("_");
			result = split[0];
		} catch (Exception e) {
			result = "";
		}
		return result;
	}

	public String getActualRound() {
		String parameter = FacesContext.getCurrentInstance()
				.getExternalContext().getRequestParameterMap().get("id");
		String[] split = parameter.split("_");
		return split[1];
	}

	/* vlozeno konec */
}
